<template>
	<view class="icons" @click.stop="likeTap(item._id)">
		<uni-icons :type="like? 'heart-filled':'heart'" size="20" color="#f07373"></uni-icons>
	</view>
</template>

<script>
	import uniIcons from "../uni-icons/uni-icons"
	export default {
		components:{
			uniIcons
		},
		props:{
			item:{
				type:Object,
				default(){
					return {}
				}
			},
			types:{
				type:String,
				default:''
			}
		},
		data() {
			return {
				like:false
			};
		},
		watch:{
			item(newVal){
				//当item变化时，this.like 重新赋值
				this.like = this.item.is_like
			}
		},
		created(){
			this.like = this.item.is_like //当渲染是可能获取不到is_like,可以使用watch监听
		},
		methods:{
			likeTap(article_id){
				console.log(this.item._id)
				this.like=!this.like
				this.setUpdateLikes(article_id)
				console.log('收藏')
			},
			setUpdateLikes(){
				uni.showLoading({
					//加载提示
				})
				this.$api.update_like({
					// user_id:this.item._id,
					article_id:this.item._id
				}).then((res)=>{
					uni.hideLoading()
					console.log(res)
					uni.showToast({
						title:this.like? "收藏成功" : "取消收藏",
						icon:"none"
					})
					uni.$emit("update_article",this.types)
				}).catch(()=>{
					uni.hideLoading()
				})
			},
		}
	}
</script>

<style>
.icons{
		position:absolute;
		right:0;
		top:0;
		display: flex;
		justify-content: center;
		align-items: center;
		width:20px;
		height:20px;
		/* border:1px solid red; */
	}
</style>
